package com.tedu.controller;

import com.tedu.manager.GameLoad;
import com.tedu.manager.MusicManager;
import com.tedu.show.GameConstants;
import com.tedu.show.GameJFrame;
import com.tedu.show.GameMainJPanel;
import com.tedu.show.menus.*;

import javax.swing.*;

/**
 * <p>游戏总控制器
 * <p>负责管理游戏状态和界面之间的切换。
 */
public class GameController {
    private static GameJFrame gameJFrame;
    public static String selectedLevel = "map1";    // 存储选择的关卡 ID
    public static boolean isMultiplayerMode = false;    // 双人模式

    // 游戏状态枚举类
    public enum GameState {
        START(MusicStateType.MENU),              // 开始界面
        SELECTION(MusicStateType.MENU),          // 选择界面
        LEVEL_SELECTION(MusicStateType.MENU),    // 关卡选择界面
        INFO(MusicStateType.MENU),               // 游戏说明界面
        SETTINGS(MusicStateType.MENU),           // 设置界面
        IN_GAME(MusicStateType.GAMEPLAY),        // 游戏中
        GAME_OVER(MusicStateType.POST_GAME);     // 游戏结束界面

        private final MusicStateType type;

        GameState(MusicStateType type) {
            this.type = type;
        }

        public MusicStateType getType() {
            return type;
        }
    }

    // 状态类别
    public enum MusicStateType {
        MENU, GAMEPLAY, POST_GAME
    }

    public static void main(String[] args) {
        // 加载游戏资源
        GameLoad.loadCoreResources();
        // 创建游戏窗体
        gameJFrame = new GameJFrame();
        // 初始化音乐管理器
        MusicManager.getInstance();
        // 默认显示开始界面
        switchPanel(GameState.START);
        // 显示窗体
        gameJFrame.setVisible(true);
    }

    /**
     * 切换界面的核心方法
     *
     * @param newState 要切换到的新状态
     */
    public static void switchPanel(GameState newState) {
        JPanel newPanel;
        // 音乐控制
        playMusicForState(newState);
        // 窗口控制
        if (newState != GameState.IN_GAME) {
            gameJFrame.updateSize(
                    GameConstants.WINDOW_WIDTH,
                    GameConstants.WINDOW_HEIGHT,
                    false
            );
        }

        switch (newState) {
            case START:
                newPanel = new StartScreen();
                break;
            case SELECTION:
                newPanel = new SelectionScreen();
                break;
            case LEVEL_SELECTION:
                newPanel = new LevelSelectionScreen();
                break;
            case INFO:
                newPanel = new InfoScreen();
                break;
            case SETTINGS:
                newPanel = new SettingsScreen();
                break;
            case IN_GAME:
                // 实例化所有游戏组件
                GameMainJPanel gameMainJPanel = new GameMainJPanel();
                GameListener gameListener = new GameListener();
                GameThread gameThread = new GameThread();

                // 将游戏组件注入到窗体中
                gameJFrame.addKeyListener(gameListener);
                gameJFrame.setThead(gameThread);
                newPanel = gameMainJPanel;
                break;
            case GAME_OVER:
                newPanel = new EndScreen();
                break;
            default:
                newPanel = new StartScreen();
                break;
        }

        gameJFrame.setjPanel(newPanel);
    }

    public static void updateWindow(int mapPixelWidth, int mapPixelHeight) {
        final int BORDER_WIDTH = 16;  // 左右边框总和
        final int TITLE_BAR_HEIGHT = 39; // 标题栏高度
        gameJFrame.updateSize(
                mapPixelWidth + BORDER_WIDTH,
                mapPixelHeight + TITLE_BAR_HEIGHT,
                true // 游戏中允许调整大小
        );
    }

    /**
     * 根据新的游戏状态播放对应的音乐
     */
    private static void playMusicForState(GameState newState) {
        MusicManager manager = MusicManager.getInstance();
        switch (newState.getType()) {
            case MENU:
                // 使用配置文件中的键名
                manager.playMusic("music.menu", true);
                break;
            case GAMEPLAY:
                switch (selectedLevel) {
                    case "map1":
                        manager.playMusic("music.level1", true);
                        break;
                    case "map2":
                        manager.playMusic("music.level2", true);
                        break;
                    case "map3":
                        manager.playMusic("music.level3", true);
                        break;
                    default:
                        manager.playMusic("music.level1", true);
                        break;
                }
                break;
            case POST_GAME:
                manager.playMusic("music.gameover", false); // 游戏结束音乐只播放一次
                break;
        }
    }
}
